import { message } from "antd";
import axios from "axios";
import { getToken } from "../storage/userToken";
const request = axios.create({
  timeout: 5000,
});
//请求拦截器
request.interceptors.request.use(
  (config) => {
    config.headers["Authorization"] = `Bearer ${getToken()}`;
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);
//响应拦截器
request.interceptors.response.use((res) => {
  const resData = (res.data || {}) as ResDataType;
  const { code, data, msg } = resData;
  if (code !== 0) {
    if (msg) {
      message.error(msg);
      throw new Error(msg);
    }
  }
  return data as any;
});
export default request;

export type ResType = {
  code: number;
  data?: ResDataType;
  msg?: string;
};
export type ResDataType = {
  [key: string]: any;
};
